package com.jic.manager.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.MeentryTerms;
import com.jic.manager.mapper.MeentryTermsMapper;
import com.jic.manager.request.MeentryTermsRequest;
import com.jic.manager.service.MeentryTermsService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 入驻条款
 * @author :
 * @email :
 * @since : 2020-02-26 09:40:14
 * @version : v1.0.0
 */
@Service
public class MeentryTermsServiceImpl implements MeentryTermsService{

    @Resource
    private MeentryTermsMapper meentryTermsMapper;

    @Override
    public long insert(MeentryTermsRequest meentryTermsRequest) {
        MeentryTerms meentryTerms = new MeentryTerms();
        BeanUtils.copyProperties(meentryTermsRequest,meentryTerms);
        meentryTerms.setCreator(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperator():0);
        meentryTerms.setCreatorName(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperateName():"");
        return meentryTermsMapper.insert(meentryTerms);
    }

    @Override
    public long delete(Long id) {
        return meentryTermsMapper.delete(id);
    }

    @Override
    public long update(MeentryTermsRequest meentryTermsRequest) {
        MeentryTerms meentryTerms = new MeentryTerms();
        BeanUtils.copyProperties(meentryTermsRequest,meentryTerms);
        meentryTerms.setUpdater(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperator():0);
        meentryTerms.setUpdaterName(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperateName():"");
        return meentryTermsMapper.update(meentryTerms);
    }

    @Override
    public MeentryTerms queryByPrimaryKey(Long id){
        return meentryTermsMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MeentryTerms> query(MeentryTerms meentryTerms){
        return meentryTermsMapper.query(meentryTerms);
    }

    @Override
    public PageResult<MeentryTerms> queryPage(MeentryTermsRequest meentryTermsRequest) {
        //计算下标
        int startIndex = (meentryTermsRequest.getStart() - 1) * meentryTermsRequest.getLimit();
        List<MeentryTerms> list = meentryTermsMapper.queryPage(startIndex, meentryTermsRequest.getLimit(), meentryTermsRequest);
        long count = meentryTermsMapper.queryPageCount(meentryTermsRequest);
        PageResult<MeentryTerms> pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public RestResult<String> editEntryTermsInfo(MeentryTermsRequest meentryTermsRequest) {
        try{
            MeentryTerms meentryTerms = new MeentryTerms();
            BeanUtils.copyProperties(meentryTermsRequest,meentryTerms);

            Long id =meentryTermsRequest.getId();
            String code =meentryTermsRequest.getEntryCode();
            Integer num =meentryTermsMapper.getTermsCountByParams(code);
            if(id !=null && id!=0){
                meentryTerms.setUpdater(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperator():0);
                meentryTerms.setUpdaterName(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperateName():"");
                MeentryTerms mems =meentryTermsMapper.queryByPrimaryKey(id);
                String codes =mems.getEntryCode();//判断是否是本条数据
                // TODO 判断符两边有一个空格
                if(codes.equals(code)||num==0){//判断编码是否唯一
                    String status = meentryTermsRequest.getStatus().toString();
                    if(status.equals("1")){//判断是否启用,如果选择启用
                        meentryTermsMapper.update(meentryTerms);

                    }else {//如果选择停用
                        meentryTermsMapper.update(meentryTerms);
                    }
                }else {
                    throw new RuntimeException("此条条款编码已经使用，请填入其他编码");
                }
            }else {
                if(num==0){
                    meentryTerms.setCreator(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperator():0);
                    meentryTerms.setCreatorName(null!=meentryTermsRequest.getAdminOperate()?meentryTermsRequest.getAdminOperate().getOperateName():"");
                    meentryTerms.setDeptId(meentryTermsRequest.getAdminOperate().getDeptId());
                    meentryTerms.setDeleteFlag(0);
                    String status = meentryTermsRequest.getStatus().toString();
                    if(status.equals("1")){//判断是否启用,如果选择启用
                        meentryTerms.setEntryType(meentryTermsRequest.getEntryType());
                        meentryTermsMapper.updateByParams(meentryTermsRequest);
                        meentryTermsMapper.insert(meentryTerms);
                    }else {//如果选择停用
                        meentryTermsMapper.insert(meentryTerms);
                    }
                }else {
                    throw new RuntimeException("此条条款编码已经使用，请填入其他编码");
                }
            }
            return RestResult.success("成功");
        }catch (Exception e){
            // TODO
            e.printStackTrace();
            return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    @Override
    public RestResult<String> deleteEntryTerms(String ids) {
        try{
            // TODO if 判断后边的大括号不能省略
            if(StringUtils.isEmpty(ids)) throw new Exception("必传参数ids为空!");
            String[] idList = ids.split(",");
            for (String id : idList) {
                meentryTermsMapper.delete(Long.valueOf(id));
            }
            return RestResult.success("成功");
        }catch (Exception e){
            // TODO
           e.printStackTrace();
           return RestResult.error("9999",e.getLocalizedMessage());
        }
    }

    @Override
    public void updateByParams(MeentryTermsRequest meentryTermsRequest) {
        meentryTermsMapper.updateByParams(meentryTermsRequest);
    }

    @Override
    public MeentryTerms getTermsInfo(MeentryTerms meentryTerms) {
        meentryTerms.setDeleteFlag(0);
        meentryTerms.setStatus(1);
        return meentryTermsMapper.getTermsInfo(meentryTerms);
    }
}
